<?php
namespace app\index\controller;

use app\index\model\CertUseListModel;
use app\index\model\ContractBianListModel;
use app\index\model\ContractBianModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractBuyListModel;
use app\index\model\ContractBuyModel;
use app\index\model\ContractCaiJieModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractDayListModel;
use app\index\model\ContractDayModel;
use app\index\model\ContractLianModel;
use app\index\model\ContractShiJieModel;
use app\index\model\ContractShiModel;
use app\index\model\DeptModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginVisaModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\ShouXiangModel;
use app\index\model\StuffRuModel;
use app\index\model\ZhiXiangModel;
use app\index\relevan\EnginRelevan;
use app\index\service\ContractService;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use think\Db;
use think\Exception;
use think\Request;

class Contract extends Common
{
    public function list_contract_yeji(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shi.deleted_user','=', 0];
        $where[] = ['shi.comid','=',$this->userinfo['comid']];
        $where[] = ['shi.isturn','=',1];
        $where[] = ['shi.status','=',1];
        $where[] = ['shi.isyeji','=','是'];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shi.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|shi.year','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['shi.year','=', $param['year']];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['jinglier_name']) && !empty($param['jinglier_name'])){
            $where[] = ['jingli.name','like', '%'.$param['jinglier_name'].'%'];
        }
        if(isset($param['jifuer_name']) && !empty($param['jifuer_name'])){
            $where[] = ['jifu.name','like', '%'.$param['jifuer_name'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['shiname']) && !empty($param['shiname'])){
            $where[] = ['shi.name','like', '%'.$param['shiname'].'%'];
        }
        if(isset($param['shihetype']) && !empty($param['shihetype'])){
            $where[] = ['shi.shihetype','=', $param['shihetype']];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['shi.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['chenger']) && !empty($param['chenger'])){
            $where[] = ['shi.chenger','=', $param['chenger']];
        }
        if(isset($param['shimoneymin']) && !empty($param['shimoneymin'])){
            $where[] = ['shi.money','>=', $param['shimoneymin']];
        }
        if(isset($param['shimoneymax']) && !empty($param['shimoneymax'])){
            $where[] = ['shi.money','<=', $param['shimoneymax']];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['shi.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['shi.enddt','between', explode(' - ', $param['enddt'])];
        }

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');
        $data['is_shihetype'] = isset($param['shihetype']) ? $param['shihetype'] : '';

        $data['chenger'] = (new UserService())->get_chaninfo_all();
        $data['is_chenger'] = isset($param['chenger']) ? $param['chenger'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = ContractShiModel::alias('shi')
            ->field('e.name as enginname,e.engintype,e.areas,e.state,e.status,shi.id
            ,shi.enginid,shi.name as shiname,shi.shihetype
            ,shi.year,shi.faer,shi.chenger,shi.money,shi.startdt,shi.enddt
            ,jingli.name as jinglier_name,jifu.name as jifuer_name')
            ->join('engin e','e.id=shi.enginid')
            ->leftJoin('person jingli','jingli.id=e.jinglier')
            ->leftJoin('person jifu','jifu.id=e.jifuer')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_shi', 'shi'))
            ->order('shi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                $item['engintype_name'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['chenger_name'] = DeptModel::get_name_by_pk($item['chenger']);
                $item['shihetype_name'] = (new OptService())->get_base_showname('engin_shihetype', $item['shihetype']);

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }


    public function list_contract_shi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shi.deleted_user','=', 0];
        $where[] = ['shi.comid','=',$this->userinfo['comid']];
        $where[] = ['shi.isturn','=',1];
        $where[] = ['shi.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shi.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|shi.name|shi.number|shi.faer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['shi.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shi.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shihetype']) && !empty($param['shihetype'])){
            $where[] = ['shi.shihetype','=', $param['shihetype']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['shi.year','=', $param['year']];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['shi.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['chenger']) && !empty($param['chenger'])){
            $where[] = ['shi.chenger','=', $param['chenger']];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['shi.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['shi.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['shi.state','=', $param['state']];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['shi.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['zhidt']) && !empty($param['zhidt'])){
            $where[] = ['shi.zhidt','between', explode(' - ', $param['zhidt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shi.status','=', $param['status']];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['chenger'] = (new UserService())->get_chaninfo_all();
        $data['is_chenger'] = isset($param['chenger']) ? $param['chenger'] : '';

        $data['state'] = (new ContractService())->get_contract_shi_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');
        $data['is_shihetype'] = isset($param['shihetype']) ? $param['shihetype'] : '';

        $data['data'] = ContractShiModel::alias('shi')
            ->field('shi.*,e.name as enginname,e.business,e.shimoney,bill.sericnum,bill.modename')
            ->join('engin e','e.id=shi.enginid')
            ->leftJoin('flow_bill bill','bill.id=shi.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_shi', 'shi'))
            ->order('shi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chenger_name'] = DeptModel::get_name_by_pk($item['chenger']);
                $item['shihetype_name'] = (new OptService())->get_base_showname('engin_shihetype', $item['shihetype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('contract_shi');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_contract_shi_tong_info(Request $request){
        $param = $request->post();

        $where[] = ['zhu.deleted_user','=', 0];
        $where[] = ['zhu.comid','=',$this->userinfo['comid']];
        $where[] = ['zhu.isturn','=',1];
        $where[] = ['zhu.status','=',1];

        $year = $request->param('year', date("Y"), 'int');
        $chanid = $request->param('chanid', (new UserService())->get_user_chanid(), 'int');

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = $chanid;
        $data['is_year'] = $year;


        $data['nianqianshifee'] = ContractShiModel::alias('zhu')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_shi', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->where(['zhu.year'=>$year])
            ->sum('e.shimoney');
        $data['nianshijiefee'] =  ContractShiJieModel::alias('zhu')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_shi_jie', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->where('zhu.jiedt','between',[$year."-01-01", $year."-12-31"])
            ->sum('zhu.money');
        $data['nianvisafee'] =  EnginVisaModel::alias('zhu')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_visa', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->where('zhu.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('zhu.money');
        $data['nianchanfee'] =  EnginChanModel::alias('zhu')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_chan', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->where('zhu.month','between',[$year."-01", $year."-12"])
            ->sum('zhu.money');
        $xmsr_gchk = (new UserService())->get_optionfeiid_bynum('xmsr_gchk');
        $data['niangchkfee'] =  ShouXiangModel::alias('zhu')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('shou_xiang', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->where(['zhu.feilei'=>$xmsr_gchk])
            ->where('zhu.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('zhu.money');
        $data['niankaifee'] =  PiaoKaiListModel::alias('list')
            ->join('piao_kai zhu','zhu.id=list.kaiid')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_kai', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->where('zhu.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('list.money');




        //合同待回款
        $htdhk['zong'] = ContractShiModel::alias('zhu')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_shi', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->sum('e.shimoney');
        $htdhk['hui'] = ShouXiangModel::alias('zhu')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('shou_xiang', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->where(['zhu.feilei'=>$xmsr_gchk])
            ->sum('zhu.money');
        $htdhk['dai'] = round($htdhk['zong'] - $htdhk['hui'], 2);
        $htdhk['dai'] = $htdhk['dai'] < 0 ? 0 : $htdhk['dai'];
        $data['htdhk'] = $htdhk;


        //自营项目年度签约趋势
        //联营项目年度签约趋势
        $data['zhe_year'] = $year;
        $data['zhe_year_shang'] = $year-1;
        $data['zhe_zi'] = [];
        $data['zhe_zi_shang'] = [];
        $data['zhe_lian'] = [];
        $data['zhe_lian_shang'] = [];
        for ($i=1; $i<=12; $i++){

            $data['zhe_zi'][] = ContractShiModel::alias('zhu')
                ->join('engin e','e.id=zhu.enginid')
                ->where($where)
                ->where((new MenuService())->get_miss_where('contract_shi', 'zhu'))
                ->where(['e.chanid'=>$chanid, 'e.business'=>1])
                ->where('zhu.qiandt','between',[$data['zhe_year']."-".$i."-01", date("Y-m-t", strtotime($data['zhe_year']."-".$i."-01"))])
                ->sum('e.shimoney');
            $data['zhe_zi_shang'][] = ContractShiModel::alias('zhu')
                ->join('engin e','e.id=zhu.enginid')
                ->where($where)
                ->where((new MenuService())->get_miss_where('contract_shi', 'zhu'))
                ->where(['e.chanid'=>$chanid, 'e.business'=>1])
                ->where('zhu.qiandt','between',[$data['zhe_year_shang']."-".$i."-01", date("Y-m-t", strtotime($data['zhe_year_shang']."-".$i."-01"))])
                ->sum('e.shimoney');

            $data['zhe_lian'][] = ContractShiModel::alias('zhu')
                ->join('engin e','e.id=zhu.enginid')
                ->where($where)
                ->where((new MenuService())->get_miss_where('contract_shi', 'zhu'))
                ->where(['e.chanid'=>$chanid, 'e.business'=>2])
                ->where('zhu.qiandt','between',[$data['zhe_year']."-".$i."-01", date("Y-m-t", strtotime($data['zhe_year']."-".$i."-01"))])
                ->sum('e.shimoney');
            $data['zhe_lian_shang'][] = ContractShiModel::alias('zhu')
                ->join('engin e','e.id=zhu.enginid')
                ->where($where)
                ->where((new MenuService())->get_miss_where('contract_shi', 'zhu'))
                ->where(['e.chanid'=>$chanid, 'e.business'=>2])
                ->where('zhu.qiandt','between',[$data['zhe_year_shang']."-".$i."-01", date("Y-m-t", strtotime($data['zhe_year_shang']."-".$i."-01"))])
                ->sum('e.shimoney');
        }




        //结算待回款
        $jsdhk['zong'] = ContractShiJieModel::alias('zhu')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_shi_jie', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->sum('zhu.money');
        $jsdhk['hui'] = $htdhk['hui'];
        $jsdhk['dai'] = round($jsdhk['zong'] - $jsdhk['hui'], 2);
        $jsdhk['dai'] = $jsdhk['dai'] < 0 ? 0 : $jsdhk['dai'];
        $data['jsdhk'] = $jsdhk;

        //结算待回款
        $kpdhk['zong'] = PiaoKaiListModel::alias('list')
            ->join('piao_kai zhu','zhu.id=list.kaiid')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_kai', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->sum('list.money');
        $kpdhk['hui'] = $htdhk['hui'];
        $kpdhk['dai'] = round($kpdhk['zong'] - $kpdhk['hui'], 2);
        $kpdhk['dai'] = $kpdhk['dai'] < 0 ? 0 : $kpdhk['dai'];
        $data['kpdhk'] = $kpdhk;

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function edit_contract_shi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            $exist = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                ->where('status','in',[0,1])->where('id','neq',$param['id'])->find();
            if($exist){
                return outputJson(4000, '该项目下已存在施工合同，不能重复创建');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入合同名称');
            }
            if(empty($param['shihetype'])){
                return outputJson(4000, '请选择合同类型');
            }
            if(empty($param['year'])){
                return outputJson(4000, '请选择合同年份');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请选择(发包方)甲方');
            }
            if(empty($param['chenger'])){
                return outputJson(4000, '请选择承包方(乙方)');
            }
            if($param['money'] == ''){
                return outputJson(4000, '请输入合同金额含税');
            }
            if($param['rate'] == ''){
                return outputJson(4000, '请输入税率');
            }
            if(empty($param['state'])){
                return outputJson(4000, '请选择合同状态');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'contract_shi') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            Db::startTrans();
            try {
                ContractShiModel::update($param);
                (new OptService())->relation_file($files,'contract_shi',$param['id']);

                $shiinfo = ContractShiModel::where(['id'=>$param['id']])->find();
                (new EnginRelevan())->set_contract_shi($shiinfo);
                (new EnginRelevan())->set_engin_vateinfo($param['enginid']);
                (new EnginRelevan())->vate_engin_file('contract_shi', $param['id']);


                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_shi', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_shi', $param['id'], '收入合同');

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractShiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['data']['chenger_info'] = (new UserService())->get_users_part_arr($data['data']['chenger'], 'dept');
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');
            $data['files'] = (new OptService())->get_file('contract_shi', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function qian_contract_shi(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['state'] == ''){
                return outputJson(4000, '请选择合同状态');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'contract_shi') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);
            Db::startTrans();
            try {
                ContractShiModel::update($param);
                (new OptService())->relation_file($files,'contract_shi',$param['id']);
                (new EnginRelevan())->vate_engin_file('contract_shi', $param['id']);

                (new OptService())->add_flow_log('签订', 'contract_shi', $param['id']);
                (new OptService())->add_log_list('签订', 'contract_shi', $param['id'], '收入合同');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractShiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['files'] = (new OptService())->get_file('contract_shi', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_contract_shi(Request $request){
        $param = $request->post();
        $data = ContractShiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractShiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_shi', $data['id'], '施工合同');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_shi(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractShiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $data['data']['chenger_name'] = DeptModel::where(['id'=>$data['data']['chenger']])->value('name');
        $data['data']['shihetype_name'] = (new OptService())->get_base_showname('engin_shihetype', $data['data']['shihetype']);

        $list = ContractBuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'shiid'=>$data['data']['id']])
            ->where('status','in',[0,1])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('contract_shi', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_contract_bu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bu.deleted_user','=', 0];
        $where[] = ['bu.comid','=',$this->userinfo['comid']];
        $where[] = ['bu.isturn','=',1];
        $where[] = ['bu.status','in',[0,1]];
        $where[] = ['bu.shiid','=',$param['shiid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bu.name|bu.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bu.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bu.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bu.status','=', $param['status']];
        }

        $data['data'] = ContractBuModel::alias('bu')
            ->field('bu.*,e.name as enginname,shi.name as shiname,bill.sericnum,bill.modename')
            ->join('contract_shi shi','shi.id=bu.shiid')
            ->join('engin e','e.id=shi.enginid')
            ->leftJoin('flow_bill bill','bill.id=bu.billid')
            ->where($where)
            ->order('bu.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['shiinfo'] = ContractShiModel::where(['id'=>$param['shiid']])->find();
        $data['butauth'] = (new MenuService())->get_butauth('contract_bu', 1);
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_contract_bu(Request $request){
        $param = $request->post();
        $data = ContractBuModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractBuModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_bu', $data['id'], '补充合同');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_bu(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractBuModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['shiinfo'] = ContractShiModel::where(['id'=>$data['data']['shiid']])->find();

        $data['files'] = (new OptService())->get_file('contract_bu', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_contract_shi_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jie.enginid','=', $param['enginid']];
        }
        if(isset($param['shiid']) && !empty($param['shiid'])){
            $where[] = ['jie.shiid','=', $param['shiid']];
        }
        if(isset($param['isreal']) && !empty($param['isreal'])){
            $where[] = ['jie.real','=', 1];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['jie.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['jietype']) && !empty($param['jietype'])){
            $where[] = ['jie.jietype','=', $param['jietype']];
        }
        if(isset($param['jiedt']) && !empty($param['jiedt'])){
            $where[] = ['jie.jiedt','between', explode(' - ', $param['jiedt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['jietype'] = (new ContractService())->get_contract_shi_jie_jietype();
        $data['is_jietype'] = isset($param['jietype']) ? $param['jietype'] : '';

        $data['data'] = ContractShiJieModel::alias('jie')
            ->field('jie.*,e.name as enginname,e.business,shi.name as shiname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=jie.enginid')
            ->leftJoin('flow_bill bill','bill.id=jie.billid')
            ->join('contract_shi shi','shi.id=jie.shiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_shi_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('contract_shi_jie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_contract_shi_jie(Request $request){
        $param = $request->post();
        $data = ContractShiJieModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractShiJieModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_shi_jie', $data['id'], '收入合同结算');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_shi_jie(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractShiJieModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['shiinfo'] = ContractShiModel::where(['id'=>$data['data']['shiid']])->find();

        $data['files'] = (new OptService())->get_file('contract_shi_jie', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_contract_cai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['cai.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['cai.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['caitype']) && !empty($param['caitype'])){
            $where[] = ['cai.caitype','=', $param['caitype']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['cai.year','like', '%'.$param['year'].'%'];
        }
        if(isset($param['caishu']) && !empty($param['caishu'])){
            $where[] = ['cai.caishu','=', $param['caishu']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['cai.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['cai.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['cai.state','=', $param['state']];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['cai.qiandt','between', explode(' - ', $param['qiandt'])];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['cai.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new ContractService())->get_contract_shi_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['caitype'] = (new ContractService())->get_contract_cai_caitype();
        $data['is_caitype'] = isset($param['caitype']) ? $param['caitype'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['caishu'] = (new ContractService())->get_contract_cai_caishu();
        $data['is_caishu'] = isset($param['caishu']) ? $param['caishu'] : '';



        $data['data'] = ContractCaiModel::alias('cai')
            ->field('cai.*,e.name as enginname,e.business,bill.sericnum,bill.modename')
            ->join('engin e','e.id=cai.enginid')
            ->leftJoin('flow_bill bill','bill.id=cai.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_cai', 'cai'))
            ->order('cai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['caitype_name'] = (new ContractService())->get_contract_cai_caitype_name($item['caitype']);
                $item['caier_name'] = DeptModel::get_name_by_pk($item['caier']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('contract_cai');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function qian_contract_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['state'] == ''){
                return outputJson(4000, '请选择合同状态');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'contract_cai') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);
            Db::startTrans();
            try {
                ContractCaiModel::update($param);
                (new OptService())->relation_file($files,'contract_cai',$param['id']);
                (new EnginRelevan())->vate_engin_file('contract_cai', $param['id']);

                (new OptService())->add_flow_log('签订', 'contract_cai', $param['id']);
                (new OptService())->add_log_list('签订', 'contract_cai', $param['id'], '支出合同');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractCaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['files'] = (new OptService())->get_file('contract_cai', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_contract_cai(Request $request){
        $param = $request->post();
        $data = ContractCaiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractCaiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_cai', $data['id'], '支出合同');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_cai(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractCaiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['data']['caitype_name'] = (new ContractService())->get_contract_cai_caitype_name($data['data']['caitype']);
        $data['data']['caier_name'] = DeptModel::get_name_by_pk($data['data']['caier']);

        $is_budget = EnginBudgetModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1])
            ->where(['status'=>1,'enginid'=>$data['data']['enginid']])->find();
        $data['data']['is_budget'] = $is_budget ? true : false;

        $bian = ContractBianModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'caiid'=>$data['data']['id']])
            ->where('status','in',[0,1])->select()->toArray();
        $data['bian'] = $bian;

        $list = ContractCaiListModel::where(['caiid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $listinfo = EnginBudgetListModel::where(['id'=>$item['listid']])->find();
            $item['listid_name'] = $listinfo['name'];

            $item['chao_num'] = false;
            $item['chao_money'] = false;

            $allnum = ContractCaiListModel::alias('list')
                ->join('contract_cai cai','cai.id=list.caiid')
                ->where(['cai.comid'=>$this->userinfo['comid'],'cai.deleted_user'=>0,'isturn'=>1])
                ->where('cai.status','in',[0,1])
                ->where(['list.listid'=>$item['listid']])->sum('list.num');
            if($allnum > $listinfo['num']){
                $item['chao_num'] = true;
            }

            $allmoney = ContractCaiListModel::alias('list')
                ->join('contract_cai cai','cai.id=list.caiid')
                ->where(['cai.comid'=>$this->userinfo['comid'],'cai.deleted_user'=>0,'isturn'=>1])
                ->where('cai.status','in',[0,1])
                ->where(['list.listid'=>$item['listid']])->sum('list.money');
            if($allmoney > $listinfo['money']){
                $item['chao_money'] = true;
            }
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('contract_cai', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_contract_bian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bian.deleted_user','=', 0];
        $where[] = ['bian.comid','=',$this->userinfo['comid']];
        $where[] = ['bian.isturn','=',1];
        $where[] = ['bian.status','in',[0,1]];
        $where[] = ['bian.caiid','=',$param['caiid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bian.title|bian.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['bian.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bian.number','like', '%'.$param['number'].'%'];
        }

        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bian.status','=', $param['status']];
        }

        $data['data'] = ContractBianModel::alias('bian')
            ->field('bian.*,e.name as enginname,cai.name as cainame,cai.number as cainumber,bill.sericnum,bill.modename')
            ->LeftJoin('contract_cai cai','cai.id=bian.caiid')
            ->LeftJoin('engin e','e.id=cai.enginid')
            ->leftJoin('flow_bill bill','bill.id=bian.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_bian', 'bian'))
            ->order('bian.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['caiinfo'] = ContractCaiModel::where(['id'=>$param['caiid']])->find();
        $data['butauth'] = (new MenuService())->get_butauth('contract_bian');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_contract_bian(Request $request){
        $param = $request->post();
        $data = ContractBianModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractBianModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_bian', $data['id'], '支出合同变更');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_bian(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractBianModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['caiinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();
        if($data['caiinfo']){
            $data['caiinfo']['caitype_name'] = (new ContractService())->get_contract_cai_caitype_name($data['caiinfo']['caitype']);
        }

        $list = ContractBianListModel::where(['bianid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['bglistid_name'] = EnginBudgetListModel::where(['id'=>$item['bglistid']])->value('name');
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('contract_bian', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_contract_cai_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jie.enginid','=', $param['enginid']];
        }
        if(isset($param['caiid']) && !empty($param['caiid'])){
            $where[] = ['jie.caiid','=', $param['caiid']];
        }
        if(isset($param['isreal']) && !empty($param['isreal'])){
            $where[] = ['jie.real','=', 1];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }

        if(isset($param['caitype']) && !empty($param['caitype'])){
            $where[] = ['cai.caitype','=', $param['caitype']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['jie.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['jietype']) && !empty($param['jietype'])){
            $where[] = ['jie.jietype','=', $param['jietype']];
        }
        if(isset($param['jiedt']) && !empty($param['jiedt'])){
            $where[] = ['jie.jiedt','between', explode(' - ', $param['jiedt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['jietype'] = (new ContractService())->get_contract_shi_jie_jietype();
        $data['is_jietype'] = isset($param['jietype']) ? $param['jietype'] : '';



        $data['data'] = ContractCaiJieModel::alias('jie')
            ->field('jie.*,e.name as enginname,e.business,cai.name as cainame,bill.sericnum,bill.modename')
            ->join('engin e','e.id=jie.enginid')
            ->leftJoin('flow_bill bill','bill.id=jie.billid')
            ->join('contract_cai cai','cai.id=jie.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_cai_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('contract_cai_jie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_contract_cai_jie(Request $request){
        $param = $request->post();
        $data = ContractCaiJieModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractCaiJieModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_cai_jie', $data['id'], '支出合同结算');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_cai_jie(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractCaiJieModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['caiinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();

//        $data['zuizhongjine']  =ContractCaiJieModel::where()

        $data['files'] = (new OptService())->get_file('contract_cai_jie', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_contract_buy(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['buy.deleted_user','=', 0];
        $where[] = ['buy.comid','=',$this->userinfo['comid']];
        $where[] = ['buy.isturn','=',1];
        $where[] = ['buy.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['buy.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['buy.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['buy.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['buytype']) && !empty($param['buytype'])){
            $where[] = ['buy.buytype','=', $param['buytype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['buy.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['buy.dt','between', explode(' - ', $param['dt'])];
        }


        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['buy.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['buytype'] = (new ContractService())->get_contract_buy_buytype();
        $data['is_buytype'] = isset($param['buytype']) ? $param['buytype'] : '';



        $data['data'] = ContractBuyModel::alias('buy')
            ->field('buy.*,e.name as enginname,e.business,bill.sericnum,bill.modename')
            ->join('engin e','e.id=buy.enginid')
            ->leftJoin('flow_bill bill','bill.id=buy.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_buy', 'buy'))
            ->order('buy.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['buytype_name'] = (new ContractService())->get_contract_buy_buytype_name($item['buytype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('contract_buy');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_contract_buy(Request $request){
        $param = $request->post();
        $data = ContractBuyModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractBuyModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_buy', $data['id'], '采购单');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_buy(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractBuyModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['data']['buytype_name'] = (new ContractService())->get_contract_buy_buytype_name($data['data']['buytype']);


        $list = ContractBuyListModel::where(['buyid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['data']['stuffrufee'] = StuffRuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['data']['enginid'],'rutype'=>2,'buyid'=>$data['data']['id']])->sum('money');
        $data['data']['zhixiangfee'] = ZhiXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['data']['enginid'],'yiju'=>'buy','yijuid'=>$data['data']['id']])->sum('money');

        $data['files'] = (new OptService())->get_file('contract_buy', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_contract_lian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['lian.deleted_user','=', 0];
        $where[] = ['lian.comid','=',$this->userinfo['comid']];
        $where[] = ['lian.isturn','=',1];
        $where[] = ['lian.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['lian.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['lian.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['lian.year','=', $param['year']];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['lian.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['myer']) && !empty($param['myer'])){
            $where[] = ['lian.myer','=', $param['myer']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['lian.state','=', $param['state']];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['lian.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['lian.status','=', $param['status']];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['myer'] = (new UserService())->get_chaninfo_all();
        $data['is_myer'] = isset($param['myer']) ? $param['myer'] : '';

        $data['state'] = (new ContractService())->get_contract_shi_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = ContractLianModel::alias('lian')
            ->field('lian.*,e.name as enginname,e.business,bill.sericnum,bill.modename')
            ->join('engin e','e.id=lian.enginid')
            ->leftJoin('flow_bill bill','bill.id=lian.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_lian', 'lian'))
            ->order('lian.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['myer_name'] = DeptModel::get_name_by_pk($item['myer']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('contract_lian');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function qian_contract_lian(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['state'] == ''){
                return outputJson(4000, '请选择合同状态');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'contract_lian') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);
            Db::startTrans();
            try {
                ContractLianModel::update($param);
                (new OptService())->relation_file($files,'contract_lian',$param['id']);
                (new EnginRelevan())->vate_engin_file('contract_lian', $param['id']);

                (new OptService())->add_flow_log('签订', 'contract_lian', $param['id']);
                (new OptService())->add_log_list('签订', 'contract_lian', $param['id'], '联营合同');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractLianModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['files'] = (new OptService())->get_file('contract_lian', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_contract_lian(Request $request){
        $param = $request->post();
        $data = ContractLianModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractLianModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_lian', $data['id'], '联营合同');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_lian(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractLianModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['data']['myer_name'] = DeptModel::where(['id'=>$data['data']['myer']])->value('name');

        $data['files'] = (new OptService())->get_file('contract_lian', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_contract_day(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['day.deleted_user','=', 0];
        $where[] = ['day.comid','=',$this->userinfo['comid']];
        $where[] = ['day.isturn','=',1];
        $where[] = ['day.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['day.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['day.name|day.number|day.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['day.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['day.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['day.year','like', '%'.$param['year'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['day.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['day.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['day.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['helei']) && !empty($param['helei'])){
            $where[] = ['day.helei','=', $param['helei']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['day.state','=', $param['state']];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['day.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['day.status','=', $param['status']];
        }

        $data['state'] = (new ContractService())->get_contract_shi_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['helei'] = (new OptService())->get_base_info('cai_contract_day_helei');
        $data['is_helei'] = isset($param['helei']) ? $param['helei'] : '';


        $data['data'] = ContractDayModel::alias('day')
            ->field('day.*,bill.sericnum,bill.modename')
            ->leftJoin('flow_bill bill','bill.id=day.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_day', 'day'))
            ->order('day.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caier_name'] = DeptModel::get_name_by_pk($item['caier']);
                $item['helei_name'] = (new OptService())->get_base_showname('cai_contract_day_helei', $item['helei']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('contract_day');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function qian_contract_day(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['state'] == ''){
                return outputJson(4000, '请选择合同状态');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'contract_day') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);
            Db::startTrans();
            try {
                ContractDayModel::update($param);
                (new OptService())->relation_file($files,'contract_day',$param['id']);


                (new OptService())->add_flow_log('签订', 'contract_day', $param['id']);
                (new OptService())->add_log_list('签订', 'contract_day', $param['id'], '日常支出合同');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractDayModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['files'] = (new OptService())->get_file('contract_day', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_contract_day(Request $request){
        $param = $request->post();
        $data = ContractDayModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractDayModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_day', $data['id'], '日常支出合同');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_day(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractDayModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['helei_name'] = (new OptService())->get_base_showname('cai_contract_day_helei', $data['data']['helei']);
        $data['data']['caier_name'] = DeptModel::get_name_by_pk($data['data']['caier']);
        $list = ContractDayListModel::where(['dayid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('contract_day', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }








}
